﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)


using System.Windows;
using Microsoft.WindowsAPICodePack.Shell;

namespace Search
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void searchSynchronButton_Click(object sender, RoutedEventArgs e)
        {
            SearchInLibraries(textBox1.Text);
        }

        private void SearchInLibraries(string searchtext)
        {
            if (searchtext == null || searchtext == string.Empty)
            {
                return;
            }

            SearchCondition searchCondition =
                SearchConditionFactory.ParseStructuredQuery(searchtext);

            ShellSearchFolder searchFolder =
                new ShellSearchFolder(
                    searchCondition,
                    (ShellContainer)KnownFolders.Libraries);

            try
            {
                listBox1.ItemsSource = searchFolder;
            }
            catch
            {
                searchFolder.Dispose();
                searchFolder = null;
            }
        }

        private void textBox1_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                SearchInLibraries(textBox1.Text);
            }
        }
    }
}
